package com.example.wangfanwangluo.wangfanboss;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.wangfanwangluo.wangfanboss.Adapters.MainAdapter;
import com.example.wangfanwangluo.wangfanboss.Models.MainModel;
import java.util.ArrayList;
import java.util.List;

@SuppressLint("ValidFragment")
public class WorkFragment extends Fragment {
    private String mTitle;
    private ListView lv;
    private MainAdapter adapter;
    private List<MainModel> list = new ArrayList<>();
    private View headerView;
    private TextView searchbtn;

    public static WorkFragment getInstance(String title) {
        WorkFragment sf = new WorkFragment();
        sf.mTitle = title;
        return sf;
    }
//390+200+16+60
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fr_work, null);
        loadData();
        searchbtn = v.findViewById(R.id.main_search_btn);
        adapter = new MainAdapter(getActivity(),list);
        lv = (ListView)v.findViewById(R.id.work_listview);
        headerView = inflater.inflate(R.layout.header_main, null);
        lv.addHeaderView(headerView);
        lv.setAdapter(adapter);

        headerListeners(v);

        searchbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //进入搜索界面
                Intent intent = new Intent(getActivity(),Search.class);
                startActivity(intent);
            }
        });

        return v;
    }

    public void headerListeners(View v) {
        LinearLayout kaidan = (LinearLayout) v.findViewById(R.id.work_kaidan);
        LinearLayout daishou = (LinearLayout) v.findViewById(R.id.work_daishou);
        LinearLayout peizai = (LinearLayout) v.findViewById(R.id.work_peizai);
        LinearLayout kucun = (LinearLayout) v.findViewById(R.id.work_kucun);
        LinearLayout fahuo = (LinearLayout) v.findViewById(R.id.work_fahuo);
        LinearLayout daohuo = (LinearLayout) v.findViewById(R.id.work_daohuo);
        LinearLayout fache = (LinearLayout) v.findViewById(R.id.work_fache);
        LinearLayout daoche = (LinearLayout) v.findViewById(R.id.work_daoche);

        kaidan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), KaidanActivity.class);
                startActivity(intent);
            }
        });

        daishou.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(),"代收开单"
                        ,Toast.LENGTH_SHORT).show();
            }
        });

        peizai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), PeizaiyundanActivity.class);
                intent.putExtra("title","津BMW123");
                startActivity(intent);
            }
        });

        kucun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), KucunActivity.class);
                startActivity(intent);
            }
        });

        fahuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(),"发货"
                        ,Toast.LENGTH_SHORT).show();
            }
        });

        daohuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(),"到货"
                        ,Toast.LENGTH_SHORT).show();
            }
        });

        fache.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), FacheActivity.class);
                intent.putExtra("type","fache");
                startActivity(intent);
            }
        });

        daoche.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), FacheActivity.class);
                intent.putExtra("type","daoche");
                startActivity(intent);
            }
        });
    }

    private void loadData() {
        for (int i = 0; i < 5; i++) {
            MainModel firstModel = new MainModel("运单号：DJFGHJHHK121212212","乌鲁木齐","拉萨","张晓丽","王晓静",
                    "衣服","12/31 10：00","已配载");
            list.add(firstModel);

            MainModel secondModel = new MainModel("运单号：MUVXCWER121222211","北京","上海","郭德纲","于谦",
                    "大褂","03/01 10：00","已到达");
            list.add(secondModel);
        }
    }
}